/* PreviousPrimary.do */

set more off

* Global options.
global OUTREG2OPTIONS tex(fragment) 2aster auto(2) rdec(3) label
global TABLELOC "."
* Clustering on state-year not working.
global FE_options fe robust //cluster(State_Year)

use "PreviousPrimary.dta", clear

*
**********************
* Set up panel.
* tsset Diff in diff by name-state-year.
**********************
*
tsset name_state year, delta(2)


*****************************
* Regression models
*****************************
* Loop over different specifications.
forvalues modelgroups = 1(1)3 {
  * Determine specification.
  if `modelgroups'==1 {
    * All observations.
    capture qui gen insample = 1
    local EVs = "prim_turn_first_nonpartisan prim_turn_third_nonpartisan prim_turn_fourth_nonpartisan"
    local outregfile "$TABLELOC/Table07"
    local note "Excluded category is closed partisan primaries. Limited to California and Washington."
  }

  local keepvars "`EVs'"
  
  * Main model with interaction.
  xtreg sum_contribs `EVs' i.prim_turn_cat i.State_Year if insample, $FE_options
   outreg2 using "`outregfile'.tex", $OUTREG2OPTIONS ctitle("Sum of,Contributions") sdec(1) addnote("OLS coefficients with robust standard errors clustered on state-election in parentheses.","`note'") nocons keep(`keepvars') addtext("State-year FEs","Yes","Name-state FEs","Yes","Previous turnout group FEs","Yes") replace
  
  * Log version.
  xtreg log_sum_contribs `EVs' i.prim_turn_cat i.State_Year if insample, $FE_options
   outreg2 using "`outregfile'.tex", $OUTREG2OPTIONS ctitle("Log,Sum,Contributions") sdec(2) bdec(2) nocons keep(`keepvars') addtext("State-year FEs","Yes","Name-state FEs","Yes","Previous turnout group FEs","Yes") append
  * Percentage change.
  foreach var of varlist `EVs' {
    di "Estimated percentage change of `var':" _n 100*(exp(_b[`var'])-1)
  }

  * Number contributions
  xtreg n_contribs `EVs' i.prim_turn_cat i.State_Year if insample, $FE_options
   outreg2 using "`outregfile'.tex", $OUTREG2OPTIONS ctitle("Number,Contributions") sdec(2) nocons keep(`keepvars') addtext("State-year FEs","Yes","Name-state FEs","Yes","Previous turnout group FEs","Yes") append

  * Log version.
  xtreg log_n_contribs `EVs' i.prim_turn_cat i.State_Year if insample, $FE_options
   outreg2 using "`outregfile'.tex", $OUTREG2OPTIONS ctitle("Log,Number,Contributions") sdec(2) nocons keep(`keepvars') addtext("State-year FEs","Yes","Name-state FEs","Yes","Previous turnout group FEs","Yes") append

  /* Don't have a strong hypothesis about which direction this should go.
  * Percentage of election-classified contributions primary.
  xtreg pct_primary `EVs' i.prim_turn_cat i.State_Year, $FE_options
   outreg2 using "`outregfile'.tex", $OUTREG2OPTIONS ctitle("Percent,Contributions,In Primary") sdec(2) nocons keep(`keepvars') addtext("State-year FEs","Yes","Name-state FEs","Yes","Previous turnout group FEs","Yes") append
  */
}
